package com.example.weatherapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

public class GuideActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private LinearLayout indicatorLayout;
    private Button btnEnter;
    private List<ImageView> indicators;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        viewPager = findViewById(R.id.viewPager);
        indicatorLayout = findViewById(R.id.indicatorLayout);
        btnEnter = findViewById(R.id.btnEnter);

        List<GuideAdapter.GuideItem> guideItems = new ArrayList<>();
        guideItems.add(new GuideAdapter.GuideItem(R.drawable.guide_1, "欢迎使用"));
        guideItems.add(new GuideAdapter.GuideItem(R.drawable.guide_2, "探索功能"));
        guideItems.add(new GuideAdapter.GuideItem(R.drawable.guide_3, "开始体验"));

        GuideAdapter adapter = new GuideAdapter(guideItems);
        viewPager.setAdapter(adapter);

        setupIndicators(guideItems.size());

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                updateIndicators(position);
                btnEnter.setVisibility(position == guideItems.size() - 1 ? View.VISIBLE : View.GONE);
            }
        });

        btnEnter.setOnClickListener(v -> {
            startActivity(new Intent(GuideActivity.this, MainActivity.class));
            finish();
        });
    }

    private void setupIndicators(int count) {
        indicators = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            ImageView indicator = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(8, 0, 8, 0);
            indicator.setLayoutParams(params);
            indicator.setImageResource(R.drawable.indicator_inactive);
            indicatorLayout.addView(indicator);
            indicators.add(indicator);
        }
        updateIndicators(0);
    }

    private void updateIndicators(int position) {
        for (int i = 0; i < indicators.size(); i++) {
            indicators.get(i).setImageResource(
                    i == position ? R.drawable.indicator_active : R.drawable.indicator_inactive
            );
        }
    }
} 